;;(load "yexp.scm")
;;(load "cyl-aux.scm")
;;(load "macro.scm")



(def/err (cyl s n)
  (let ((res-str "")
	(res-cyl '())
	(st #f)
	(deli-str "")
	(res-deli-st #f)
	(define-func-name/flag #f)
	(cyl-l '())
	(cb-cyl-l '())
	(fd-cyl-l '())
	(mc-cyl-l '())
	(str-l '())
	(cb-str-l '())
	(fd-str-l '())
	(mc-str-l '())
	(mc-deli-st #f)
	(last-str "")
	(last-cyl '())
	(last-st #f)
	(last-fn "")
	(last-par-num 0)
	(s-len (string-length s))
	(deli-cyl '())
	)
    (pre-cyl-init)
    (if (> s-len 0)
	(begin
	  (if (rd-p-deli (string-ref s 0))
	      (begin
		(let-set! (deli-str n st) (get-deli s 0 0 '开始 ))
		(if (eq? st '结束分隔符)
		    (set! n (+ n 1))
		    (exit-cyl (list 'err  0  (format  "不允许首词语是其它分隔符号，只允许是结束分隔符。[~a]" deli-str))))))
	  (set! cyl-define-status 'top-level)
	  (set! st '开始)
	  (if (>= n s-len) (exit-cyl (list 'err 0 'cyl (format "不允许仅为分隔符的字符串。"))))
 
	  (tr-string (loc c s n)
	       ;;(y-or-n "CYL-tr-string:loc=~a,last-cyl=~a,cyl-l=~a\n" loc last-cyl cyl-l)
	       (cond
		((rd-p-deli c)
		 (let-set! (deli-str loc st) (get-deli s loc loc '开始))
		 ;;(if (and (string? last-str)(string=? last-str "下一字符")) (y-or-n "CYL-deli:deli-st=~a,cyl-l=~a,fd-cyl-l=~a,cb-cyl-l=~a.status=~a,loc=~a\nundefined-list=~a\n" st cyl-l fd-cyl-l cb-cyl-l cyl-define-status loc cyl-undefined-symbol-list))
		 
		 (cond
		  ((and cyl-line-begin (eq? cyl-define-status 'top-level)(eq? st '函数定义分隔符))
		   ;;(y-or-n "CYL-func-define:last-cyl=~a,st=~a,current-dict=~a\n" last-cyl st cyl-current-symbol-dict-list)
		   (set! last-cyl (set-define-func-name-cyl/cyl-aux))
		   ;;(y-or-n "CYL-define0:cur-name=~a,dict=~a,dict-backup=~a.\n" cyl-current-define-function-name cyl-current-symbol-dict-list cyl-local-symbol-backup-list)
		   (if define-func-name/flag
		       (begin
			 (set! cyl-define-status 'func-define)
			 (push last-cyl fd-cyl-l)
			 (push (list deli-str (string-length deli-str) deli-return-tab-coding '函数定义分隔符 (cyl-serial)) fd-cyl-l)
			 (set! fd-str-l cb-str-l)
			 (push deli-str fd-str-l)
			 (set! cb-cyl-l '())
			 (set! cb-str-l '())
			 
			 (set! st '函数名定义结束)
			 ;;(y-or-n "CYL-define1:fd-cyl-l=~a,cyl-define-status=~a\n" fd-cyl-l cyl-define-status)
			 )
		       (begin
			 (err-suggest 'cyl loc "函数名定义位置不是一阶纯y表达式[~a]。程序将其视为top-level命令处理。" last-cyl)
			 (run/err rewrite-cyl/pure-yexp->syntax cb-cyl-l)
			 (if (string? cyl-adverb-value)
			     (let ((cyl-tl '())(str-tl '()))
			       (push (pop cb-cyl-l) cyl-tl)
			       (push (pop cb-str-l) str-tl)
			       (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) cyl-tl)
			       (push deli-str str-tl)
			       (set! cyl-line-begin #f)
			       (handle-macro/cyl-aux cyl-tl str-tl))
			     (begin
			       (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) cb-cyl-l)
			       (push deli-str cb-str-l)
			       (set! cyl-line-begin #f)
			       ))
			 ))
		   (set! cyl-line-begin #f)
		   )
		  ((and (eq? cyl-define-status 'func-define)(eq? st '函数定义分隔符))
		   (run/err rewrite-cyl/pure-yexp->syntax cb-cyl-l)
		   (if (string? cyl-adverb-value)
		       (let ((cyl-tl '())(str-tl '()))
			 (push (pop cb-cyl-l) cyl-tl)
			 (push (pop cb-str-l) str-tl)
			 (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) cyl-tl)
			 (push deli-str str-tl)
			 (set! cyl-line-begin #f)
			 (handle-macro/cyl-aux cyl-tl str-tl))
		       (begin
			 (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) cb-cyl-l)
			 (push deli-str cb-str-l)
			 (set! cyl-line-begin #f)
			 ))
		   )
		  ((and (not cyl-line-begin)(eq? st '函数定义分隔符))
		   (run/err rewrite-cyl/pure-yexp->syntax cb-cyl-l)
		   (if (string? cyl-adverb-value)
		       (let ((cyl-tl '())(str-tl '()))
			 (push (pop cb-cyl-l) cyl-tl)
			 (push (pop cb-str-l) str-tl)
			 (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) cyl-tl)
			 (push deli-str str-tl)
			 (set! cyl-line-begin #f)
			 (handle-macro/cyl-aux cyl-tl str-tl))
		       (begin
			 (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) cb-cyl-l)
			 (push deli-str cb-str-l)
			 (set! cyl-line-begin #f)
			 ))
		   )
		  ((and (eq? cyl-define-status 'top-level)(eq? st '结束分隔符))
		   ;;(y-or-n "CYL-stop-deli-top1:cyl-l=~a,cb-cyl-l=~a.status=~a\n" cyl-l cb-cyl-l cyl-define-status)
		   (run/err rewrite-cyl/pure-yexp->syntax cb-cyl-l)
		   (run/err make-cyl-list/top-aux)
		   ;;(y-or-n "CYL-stop-deli-top2:cyl-l=~a,cb-cyl-l=~a.status=~a\n" cyl-l cb-cyl-l cyl-define-status)
		   (push (list deli-str (string-length deli-str) deli-return-coding '结束分隔符 (cyl-serial)) cyl-l)
		   (push deli-str str-l)
		   
		   ;;(y-or-n "CYL-stop-deli3:cyl-l=~a,cb-cyl-l=~a,loc=~a.\n" cyl-l cb-cyl-l loc)
		   (set! cyl-define-status 'top-level)
		   (set! cyl-line-begin #t)
		   )
		  ((and (eq? cyl-define-status 'func-define)(eq? st '结束分隔符))
		   ;;(y-or-n "CYL-stop-deli-func1:cyl-l=~a,fd-cyl-l=~a,cb-cyl-l=~a.\n" cyl-l fd-cyl-l cb-cyl-l)
		   (run/err rewrite-cyl/pure-yexp->syntax cb-cyl-l)
		   ;;(y-or-n "CYL-stop-deli-func2:cyl-l=~a,fd-cyl-l=~a,cb-cyl-l=~a.\n" cyl-l fd-cyl-l cb-cyl-l)
		  
		   ;;(y-or-n "CYL-stop-deli-func3:cyl-l=~a,fd-cyl-l=~a,cb-cyl-l=~a.\n" cyl-l fd-cyl-l cb-cyl-l)
		   (push (list deli-str (string-length deli-str) deli-return-coding '结束分隔符 (cyl-serial)) cb-cyl-l)
		   (push deli-str cb-str-l)
		   (run/err make-cyl-list/func-aux)
		   ;;(y-or-n "CYL-stop-deli-func4:cyl-l=~a,fd-cyl-l=~a,cb-cyl-l=~a.\n" cyl-l fd-cyl-l cb-cyl-l)
		   (set! cyl-define-status 'top-level)
		   (set! cyl-line-begin #t)
		   )
		  ((eq? st '语句终结分隔符)
		   ;;maybe not occur here.
		   (exit-cyl (list 'err loc (merge-num loc last-cyl 1) '非法语法 (err-format 'cyl loc "语句终结分隔符不应该出现在cyl中，应该在ymacro中被处理。")))
		   )
		  ((eq? st '分隔符)
		  
		   (run/err rewrite-cyl/pure-yexp->syntax cb-cyl-l)
		   (if (string? cyl-adverb-value)
		       (let ((cyl-tl '())(str-tl '()))
			 (push (pop cb-cyl-l) cyl-tl)
			 (push (pop cb-str-l) str-tl)
			 (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) cyl-tl)
			 (push deli-str str-tl)
			 (set! cyl-line-begin #f)
			 (handle-macro/cyl-aux cyl-tl str-tl))
		       (begin
			 (push (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)) cb-cyl-l)
			 (push deli-str cb-str-l)
			 (set! cyl-line-begin #f)
			 ))
		   )
		  (else
		   ;;maybe not get here.
		   (exit-cyl (list 'err loc (merge-num loc last-cyl 1) '非法语法 (err-format 'cyl loc "[~a]非法分隔符。" deli-str)))
		   )
		  ))
		(else
		 ;;(y-or-n "CYL1:loc=~a\n" loc)
		 (let-set!  (last-str loc ls rs last-cyl last-st last-fn last-par-num) (run/err yexp s loc 0 0))
		 ;;(y-or-n "CYL2:loc=~a,last-cyl=~a,last-fn=~a.\n" loc last-cyl last-fn)
		   (cond
		    ((eq? last-st '系统宏)
		     (handle-macro/cyl-aux cb-cyl-l cb-str-l)
		     ;;(y-or-n "cyl-macro:loc=~a\n" loc)
		     )
		    (else
		     (push last-cyl cb-cyl-l)
		     (push last-str cb-str-l))))
		))
	  (run/err rewrite-cyl/pure-yexp->syntax cb-cyl-l)
	  ;;handle after string traveling.
	  (let ((loc (string-length s))
		(cur-str "")
		)
	    ;;(y-or-n "CYL_CB0:fd-cyl-l=~a,cyl-l=~a.cb-cyl-l=~a" fd-cyl-l cyl-l cb-cyl-l)
	    (cond
	     ((and (pair? fd-cyl-l) (null? cb-cyl-l))
	      ;;(y-or-n "CYL_fd:fd-cyl-l=~a,cb-cyl-l" fd-cyl-l cb-cyl-l)
	      (err-suggest 'cyl loc "函数定义没有函数体，函数体将被定义为空。")
	      (set! cb-cyl-l (list "" 0 sys-nil '系统常量 (cyl-serial)))
	      (set! cb-cyl-l (append (merge-list loc fd-cyl-l) (list cb-cyl-l 0 0 '函数体定义 (cyl-serial))))
	      (push (list cb-cyl-l (merge-num loc cb-cyl-l 1) 0 '函数定义 (cyl-serial)) cyl-l)
	      (push (merge-str loc fd-str-l) str-l)
	      (handle-local-dict-end-func)
	      )
	     ((and (pair? fd-cyl-l) (pair? cb-cyl-l))
	      ;;(y-or-n "CYL_fd and cb0:fd-cyl-l=~a,cb-cyl-l=~a" fd-cyl-l cb-cyl-l)
	      (run/err rewrite-cyl/pure-yexp->syntax cb-cyl-l)	
	      ;;(y-or-n "CYL_fd and cb1:fd-cyl-l=~a,cb-cyl-l=~a" fd-cyl-l cb-cyl-l)
	      (set! cb-cyl-l (merge-list loc cb-cyl-l))
	      (set! cb-cyl-l (list cb-cyl-l (merge-num loc cb-cyl-l 1) 0 '函数体定义 (cyl-serial)))
	      ;;(y-or-n "CYL_fd and cb2:fd-cyl-l=~a,cb-cyl-l=~a" fd-cyl-l cb-cyl-l)
	      (set! cb-cyl-l (append (merge-list loc fd-cyl-l) (list cb-cyl-l)))
	      ;;(y-or-n "CYL_fd and cb3:fd-cyl-l=~a,cb-cyl-l=~a" fd-cyl-l cb-cyl-l)
	      (push (list cb-cyl-l (merge-num loc cb-cyl-l 1) 0 '函数定义 (cyl-serial)) cyl-l)
	      (set! cur-str (string-append (merge-str loc fd-str-l) (merge-str loc cb-str-l)))
	      (push cur-str str-l)
	      (handle-local-dict-end-func)
	      )
	     ((and (null? fd-cyl-l) (pair? cb-cyl-l))

	      ;;(y-or-n "CYL****:cb-cyl-l=~a\n" cb-cyl-l)
	      (run/err rewrite-cyl/pure-yexp->syntax cb-cyl-l)
	      ;;(y-or-n "CYL_cb:fd-cyl-l=~a,cb-cyl-l=~a,cb-str-l=~a.\n" fd-cyl-l cb-cyl-l cb-str-l)
	      (run/err make-cyl-list/top-aux)
	      ;;(y-or-n "CYL_CB2:cyl-l=~a,str-l=~a.cb-cyl-l=~a,cb-str-l=~a.\n" cyl-l str-l cb-cyl-l cb-str-l)
	      )
	     ((and (null? fd-cyl-l)(null? cb-cyl-l))
	      ;;(y-or-n "CYL_null:fd-cyl-l=~a,cb-cyl-l" fd-cyl-l cb-cyl-l)
	      )
	     (else (list 'err loc 0 '非法语法 (err-format 'cyl loc "开发错误，不应该到达此处。")))
	     )
	    ;;(y-or-n "CYL_END:str-l=~a,cyl-l=~a" str-l cyl-l)
	    (list (merge-str loc str-l) (merge-list loc cyl-l)))))))
      
	
	
		  
		     
		     
		   
		   
		   
		   
			 
		 
		 
   
